package com.tsd.jxc.goods.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.alibaba.fastjson.JSON;
import javax.annotation.Resource;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.tsd.system.entity.SysUser;
import com.tsd.jxc.goods.entity.JxcGoodsAttrGroup;
import com.tsd.jxc.goods.entity.JxcGoodsAttrGroupExt;
import com.tsd.jxc.goods.service.JxcGoodsAttrGroupService;

import com.tsd.common.controller.BaseController;
/**
 * 描述：JxcGoodsAttrGroup商品属性组 控制层
 *
 * @author david
 * @date 2024/05/08 10:06:10
 */
@Controller
@Scope("prototype")
@RequestMapping("/api")
@Api(tags = "进销存-商品属性组", description = "商品属性组的API描述")
public class JxcGoodsAttrGroupController extends BaseController {

    @Resource
    private JxcGoodsAttrGroupService jxcGoodsAttrGroupService;

    @ApiOperation(value="根据sid获取商品属性组", notes="根据sid获取商品属性组")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "sid", value = "商品属性组sid", required = true, dataType = "string",paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getJxcGoodsAttrGroupBySid.action", method = RequestMethod.POST)
    public HttpResult<JxcGoodsAttrGroup> getJxcGoodsAttrGroupBySid() {
        HttpResult<JxcGoodsAttrGroup> hr = new HttpResult<JxcGoodsAttrGroup>();
		try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map,"sid");
			super.checkEmpty(sid, "请提供参数sid");
			JxcGoodsAttrGroup jxcGoodsAttrGroup = jxcGoodsAttrGroupService.queryBySid(sid);
			hr.setData(jxcGoodsAttrGroup);
			hr.setCode(ErrorCode.CODE_SUCCESS);
		} catch (Exception e) {
			super.procException(hr, e);
		}
		return hr;
    }

    @ApiOperation(value="根据id删除商品属性组", notes="根据id删除商品属性组")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "sids", value = "商品属性组sids", required = true, dataType = "string",paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteJxcGoodsAttrGroups.action", method = RequestMethod.POST)
    public HttpResult<String> deleteJxcGoodsAttrGroups() {
        HttpResult<String> hr = new HttpResult<>();
		try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get("sids");
            super.checkEmpty(sids, "请提供参数sids");
            List<String> list = JSON.parseArray(JSON.toJSONString(sids),String.class);
			jxcGoodsAttrGroupService.deleteJxcGoodsAttrGroups(list, opUser);
			hr.setCode(ErrorCode.CODE_SUCCESS);
		} catch (Exception e) {
			super.procException(hr, e);
		}
		return hr;
    }


    @ApiOperation(value="保存商品属性组", notes="保存商品属性组，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "商品属性组", required = true,paramType = "body",dataType = "JxcGoodsAttrGroup")
    })
    @ResponseBody
    @RequestMapping(value = "/saveJxcGoodsAttrGroup.action", method = RequestMethod.POST)
    public HttpResult<String> saveJxcGoodsAttrGroup() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            JxcGoodsAttrGroup t = JSON.parseObject(json, JxcGoodsAttrGroup.class);
            jxcGoodsAttrGroupService.saveJxcGoodsAttrGroup(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value="分页查询商品属性组", notes="分页查询商品属性组")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string",paramType = "form"),
        @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int",paramType = "form"),
        @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int",paramType = "form"),
        @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String",paramType = "form"),
        @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int",paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findJxcGoodsAttrGroups.action", method = RequestMethod.POST)
    public HttpResult<List<JxcGoodsAttrGroupExt>> findJxcGoodsAttrGroups() {
    	HttpResult<List<JxcGoodsAttrGroupExt>> hr = new HttpResult<List<JxcGoodsAttrGroupExt>>();
		try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
			Map<String, Object> params = new HashMap<>();
            Integer dr = HlpUtils.getInteger(map, "dr", 0);
            params.put("dr", dr);
            HlpUtils.copyStringValue(map,params,"search_value","search_value",false,true);
			PageBean page = new PageBean(map);
			List<JxcGoodsAttrGroupExt> list = jxcGoodsAttrGroupService.findJxcGoodsAttrGroups(params, page);
			hr.setData(list);
			hr.setPage(page);
			hr.setCode(ErrorCode.CODE_SUCCESS);
		} catch (Exception e) {
			super.procException(hr, e);
		}
		return hr;
    }

}
